using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace GameEngine.Units
{
	public class KillUnit : AIScript
	{
		public KillUnit(Unit target)
		{
			this.Target = target;
		}

		public Unit Target { get; private set; }

		protected internal override void Update(ref GameTime gameTime)
		{
			if (!this.Target.IsDead && !this.Owner.IsDead) // Fight until one of us die!
			{
				bool isClose = Vector3.Distance(this.Owner.Position, this.Target.Position) - this.Owner.Radius - this.Target.Radius <= 10;
				// this depends on the unit skills but the logic is clear - if ranged move within range, if melee move within melee range, if close enough - kill kill kill
				if (this.Owner.IsIdle && !isClose)
				{
					this.Owner.MoveTo(this.Target, 10); // hardcoded melee range
				}
				else if (isClose)
				{
					if (this.Owner.Action is MoveTo || this.Owner.IsIdle)
					{
						// TODO: once again - implement a way to issue orders... what if the Action was Die or Stun or whatever...
						Vector3 direction = this.Target.Position - this.Owner.Position;
						this.Owner.Rotation = (float)Math.Atan2(direction.Y, direction.X);
						this.Owner.Action = new ComboAttack(this.Owner);
					}
				}
			}
		}
	}
}
